package com.yylending.las.lending.outlet;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.lending.process.confirmloan.ConfirmLoanPresenter;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 门店列表
 * Created by laixj on 2017/11/4.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class OutletPresenter extends Presenter<OutletModel, OutletPresenter.View> {
	private static final String TAG = OutletPresenter.class.getSimpleName();

	public static class Generator implements PresenterFactory<OutletPresenter> {
		@Override
		public OutletPresenter create() {
			return new OutletPresenter();
		}
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(OutletPresenter.View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new OutletModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		/**
		 * 获取门店地址成功
		 * @param outlet
		 */
		void getOutletInfoSuccess(OutletModel.Outlet outlet);
		/**
		 * 获取门店地址失败
		 * @param tips
		 */
		void getOutletInfoFailure(String tips);
	}

	public void getOutletInfo() {
		execute(null, OutletModel.FUNCTION_CODE_GET_OUTLET_LIST, true, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void outletResult(OutletModel.OutletInfoResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				if(null != result.getData() && result.getData().list.size() > 0){
					mView.getOutletInfoSuccess(result.getData().list.get(0));
				}else{
					mView.getOutletInfoFailure("门店查找失败");
				}
			} else {
				mView.getOutletInfoFailure(result.getRetMsg());
			}
		}
	}
}
